package com.karaoke.android.kege.vm

import android.app.Fragment
import android.databinding.ObservableArrayList
import android.databinding.ObservableInt
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import com.karaoke.android.data.NetworkTools
import com.karaoke.android.data.domain.DataDictionary
import com.karaoke.android.data.domain.QSong
import com.karaoke.android.data.onUI
import com.karaoke.android.kege.BR
import com.karaoke.android.kege.R
import com.karaoke.android.kege.fragment.KegeFragments
import com.karaoke.android.library.databind.ItemBinding
import com.karaoke.android.library.mvvm.FragmentViewModel
import com.trello.rxlifecycle.kotlin.bindToLifecycle
import rx.Observable
import universum.studios.android.fragment.manage.FragmentController
import universum.studios.android.fragment.manage.FragmentRequest
import java.util.concurrent.TimeUnit
import kotlin.properties.Delegates

/**
 * Created by allen on 2017/4/23.
 */

class CategoryArtsViewModel(val f: Fragment) : FragmentViewModel(), AdapterView.OnItemClickListener
{
    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long)
    {
        if (selectedPosition.get() != position)
        {
            controller.newRequest(KegeFragments.SONG_PAGE).arguments(Bundle().apply {
                putInt("TypeId", langs[position].id)
                putString("QueryType", QSong.QueryType.QUERY_SONG_TYPE.name)
            }).transaction(FragmentRequest.REPLACE)
                    .tag("song_page_${langs[position].id}")
                    .execute()
        }
        selectedPosition.set(position)
    }

    val selectedPosition = ObservableInt().apply { set(-1) }
    protected var controller by Delegates.notNull<FragmentController>()

    init
    {

        controller = FragmentController(f.childFragmentManager)
        controller.viewContainerId = R.id.sContainer
        controller.factory = KegeFragments()
    }

    override fun onInitLoad()
    {
        super.onInitLoad()
        NetworkTools.getDictByPid(42).bindToLifecycle(this).onUI({
                                                                     langs.addAll(it)
                                                                     Observable.just(selectedPosition.set(0)).delay(300, TimeUnit.MILLISECONDS)
                                                                     controller.newRequest(KegeFragments.SONG_PAGE).arguments(Bundle().apply {
                                                                         putInt("TypeId", it[0].id)
                                                                         putString("QueryType", QSong.QueryType.QUERY_SONG_TYPE.name)
                                                                     }).transaction(FragmentRequest.REPLACE)
                                                                             .tag("song_page_${it[0].id}")
                                                                             .execute()
                                                                 }, { })
    }

    val langs = ObservableArrayList<DataDictionary>()
    val menuBindings = ItemBinding.of<DataDictionary>(BR.item, R.layout.menu_list_item)

}
